
/**
 * enterpriseRemind

 */
//图标属性


var remindAnalyzes = {
    init : function(){
	    	this.initEvent();
	    	this.loadDriverList();
	    	this.loadEnterpriseList();
	    this.initQueryTime();//初始化查询时间
	    this.barChart = this.initBarChart();
	    this.queryBarChartData();
	    this.queryRemindTable();  
	    this.bindQuery();
	      //  $('.query-select').select2({minimumResultsForSearch: -1});
    },

    initEvent:function(){
	    	$("#remindObj").on('change',function(){
				var chose = $("#remindObj").val();
				if (chose == "1") {  //"1"代表的是企业 2代表的是司机
					//me.queryChose="enterprise";
					$("#selectEnterprise").show();
					$("#selectDriver").hide();
				}else if (chose == "2") {
				//	me.queryChose="driver";
					$("#selectEnterprise").hide();
					$("#selectDriver").show();
				}
			});
    },
    
    //加载司机列表
    loadDriverList:function(){
     	$("#selectDriverSel").select2({
	        theme: "default",
	        allowClear: true,
	        placeholder: "全部司机",
	        ajax:{
	            url:appCtx + "report/enterpriseRemind/queryByType.html?flag=2",
	            dataType:"json",
	            delay:250,
	            data:function(params){
	                return {
	                    name: params.term,
	                    page: params.page || 1
	                };
	            },
	            cache:true,
	            processResults: function (res, params) {
			    	if(res){
			    		var options = [];
			    		var option = {"id":" ", "text":"全部司机"};
			    		options.push(option);
	    				$.each(res,function(index,obj) {
	    					var option = {"id":obj.id, "text":obj.name};
    	                    options.push(option);
	    				});
    	                return {
    	                	results: options,
    	                    pagination: {
    	                        more:false
    	                    }
    	                };
			    	}
	            },
	            escapeMarkup: function (markup) { return markup; },
	            minimumInputLength: 1
	        }
	    });
    },
    //加载企业列表
    loadEnterpriseList:function(){
     	$('#selectEnterpriseSel').select2({
    		    theme: "default",
 	        allowClear: true,
 	        placeholder: "全部企业",
 	        ajax:{
 	            url:appCtx + "report/enterpriseRemind/queryByType.html?flag=1",
 	            dataType:"json",
 	            delay:250,
 	           async : true,
 	            data:function(params){
 	                return {
 	                    name: params.term,
 	                    page: params.page || 1
 	                };
 	            },
 	            cache:true,
 	            processResults: function (res, params) {
 			    	if(res){
 			    		var options = [];
 			    		var option = {"id":" ", "text":"全部企业"};
 			    		options.push(option);
 	    				$.each(res,function(index,obj) {
 	    					var option = {"id":obj.id, "text":obj.name};
     	                    options.push(option);
 	    				});
     	                return {
     	                	results: options,
     	                    pagination: {
     	                        more:false
     	                    }
     	                };
 			    	}
 	            },
 	            escapeMarkup: function (markup) { return markup; },
 	            minimumInputLength: 1
 	        }
    	})
	     /*$.ajax({
	    	 type:'get',
	         url: appCtx + "/report/enterpriseRemind/queryByType.html?flag=1",
	         cache: true,
	         async : true,
	         dataType: "JSON",
	         success: function (data) {			
	 			if(data){  				
	 				var selectStr="";
	 				selectStr+="<option value=''>全部企业</option>";
	 				$.each(data,function(entryIndex,entry) {
	 					selectStr+="<option value='"+entry.id+"'>"+entry.name+"</option>";
	 				});
	 				$('#selectEnterpriseSel').html(selectStr);
	 				$("#selectEnterpriseSel").select2();
	 			}
	         }
	     });*/
    },
    
    
    //初始化查询时间
    initQueryTime : function(){
	    	//初始化显示前一天
	    	var date = new Date();
		    var y = date.getFullYear();    
		    var m = date.getMonth() + 1;    
		    m = m < 10 ? ('0' + m) : m;    
		    var d = date.getDate();    
		    d = d < 10 ? ('0' + d) : d; 
	    	var day = y+"-"+m+"-"+d;
	    	$("#inputStartDate").val(day);
	    	$("#inputEndDate").val(day);
    	
    },
    
    // 获取查询条件
    getparams: function () {
	    	var notifyType = $('#notifyType').val(); //通知的方式
	    	var notifyObj = $('#remindObj').val(); //通知的对象
	    	var enterpriseId = $("#selectEnterpriseSel").val(); //企业的名称
	    var driverId = $.trim($("#selectDriverSel").val()); //司机的名称
	    var startDate = $("#inputStartDate").val() + " 00:00:00";
	    var endDate = $("#inputEndDate").val() + " 23:59:59";
	    	
	    var params = [ {
				name : 'notifyType',
				value : notifyType
			},{
				name : 'notifyObj',
				value : notifyObj
			},{
				name : 'startTime',
				value : startDate
			},{
				name : 'endTime',
				value : endDate
			}];
	        if($('#selectDriver').is(':hidden')){
		        	params.push({
		    			name:'objId',
		    			value:enterpriseId
		    		})
		        }else{
		        	params.push({
		    			name:'objId',
		    			value:driverId
		    		});
	        }
	
	    	return params;
    },
	
	/**
	 * 柱状图
	 */
   initBarChart : function(){
        	var me = this;
     	//按照查询条件加载柱状图
     	var option = me.getChartOption();
        var myChart = echarts.init(document.getElementById('chart'));
        myChart.setOption(option);
        window.onresize = myChart.resize;
        myChart.on('click', function(param){
	        	var notifyType = 0;
	        	var notifyObj = 0;
	        	var enterpriseOrDriverList = me.enterpriseOrDriverList;
	        	var name = enterpriseOrDriverList[param.dataIndex];
	        	if(param.seriesName == '电话'){
	        		notifyType=1;
	        	}else if(param.seriesName == '邮件'){
	        		notifyType=2;
	        	}else if(param.seriesName == '短信'){
	        		notifyType=3;
	        	} 	
	        	var startDate = $('#inputStartDate').val()+" 00:00:00";
	        	var endDate = $('#inputEndDate').val()+" 23:59:59";
	        	var notifyObj = $('#remindObj').val(); //通知的对象
	        	var objId= "";
				if($('#selectDriver').is(':hidden')){
			        	objId = $("#selectEnterpriseSel").val();
			     }else{
			        	objId = $.trim($("#selectDriverSel").val());
			        }
	        	$("#resultTable").flexOptions({
		            	url: appCtx + "report/enterpriseRemind/loadTableData.html?time="+ new Date().getTime(),
		            	params: [{
		                    name: 'notifyType',
		                    value: notifyType
		                }, {
		                	name:"notifyObj",
		            		value:notifyObj
		                },{
		                	name:"name",
		                	value:name
		                }, {
		                    name:'objId',
		                    value:objId
		                },{
		                	name:"startTime",
		            		value:startDate
		                }, {
		                    name: 'endTime',
		                    value: endDate
		                }],//设置查询参数
		                newp: 1
		            }).flexReload();
        });
        return {
	        	refash : function(option){
	                myChart.setOption(option);
	       	},
        	option : option,
        	barChartDom : myChart
        };
    },
    tableReload : function(){
    	$("#resultTable").flexOptions({
        	url: appCtx + "report/enterpriseRemind/loadTableData.html",
        	params: remindAnalyzes.getparams(),//设置查询参数
            newp: 1
        }).flexReload();
    },
    
    getChartOption : function(){
	   // 	var chartNotifyType=0;
	    	var chartColor;
	    	var chartData;
	    	var chartSeries;
	    var 	chartNotifyType = $('#notifyType').val(); //通知的方式
	    	if(chartNotifyType == 3){
	    		chartColor=['#4F81BD'];
	    		chartData=['短信'];
	    		chartSeries=[{
		            name:'短信',
		            type:'bar',
		            barMaxWidth : 100,
		            data:[],
		            itemStyle : { normal: {label : {show: true, position: 'top'}}},
	        	 }];
	    	}else if(chartNotifyType == 2){
	    		chartColor=['#FF7F00'];
	    		chartData=['邮件'];
	    		chartSeries=[{
			        name:'邮件',
			        type:'bar',
			        data:[],
			        barMaxWidth : 100,
			        itemStyle : { normal: {label : {show: true, position: 'top'}}},   
	             }];
	    	}else if(chartNotifyType == 1){
	    		chartColor=['#FF0000'];
	    		chartData=['电话'];
	    		chartSeries=[{
				     name:'电话',
				     type:'bar',
				     data:[],
				     barMaxWidth : 100,
				     itemStyle : { normal: {label : {show: true, position: 'top'}}},   
		        }];
	    	}else{
	    		chartColor = ['#4F81BD','#FF7F00',' #FF0000'];
	    		chartData = ['短信','邮件','电话'];
	    		chartSeries = [{
		            name:'短信',
		            type:'bar',
		            barMaxWidth : 100,
		            data:[],
		            itemStyle : { normal: {label : {show: true, position: 'top'}}},
	        	 },{
			        name:'邮件',
			        type:'bar',
			        data:[],
			        barMaxWidth : 100,
			        itemStyle : { normal: {label : {show: true, position: 'top'}}},   
	             },{
				     name:'电话',
				     type:'bar',
				     data:[],
				     barMaxWidth : 100,
				     itemStyle : { normal: {label : {show: true, position: 'top'}}},   
		        }
	    ];
    	}
        var option = {
        		/*title:{text:'企业问题车辆统计',x:'center',textStyle:{fontSize:'12px'} },*/
        		color : chartColor,
        		tooltip : {
	                trigger: 'axis',
	            },
	            legend: {
	                data:chartData
	            },
	            grid: {
			        left: 10,
			        top:'10%',
			        right: 12,
			        containLabel: true
			    },
	            xAxis : [
	                {	
	                	type : 'category',
			            data : []
	                }
	            ],
	            yAxis : [
	                {
	                    type : 'value',
	                }
	            ],
	            series : chartSeries,
			    dataZoom: [
			    	{
			    	type:'slider' ,
			        start: 0,
			        end: 100,
			        fillerColor:'#A3E1A3',
			        handleColor:'#83A7D1',
			        showDetail:false
			    }]
	        };
        return option;
    },
    
	/**
	 * 加载柱状图数据
	 */
	queryBarChartData: function(){
		var me = this;
		$.ajax({
			type : 'post',
			data:remindAnalyzes.getparams(),
			url : appCtx+"report/enterpriseRemind/loadChartData.html",
			cache : false,
			dataType : "JSON",
			success : function(data) {
				var option = me.barChart.option;
				var notifyType = $('#notifyType').val();
				if(data != null){
					option.xAxis[0].data =[data.xAxis]; //['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
					if(notifyType==3){  //短信
						option.legend.data=['短信'];
						option.series[0].data =data.ySMS;// [10, 52, 200, 334, 390, 330, 220];
					    option.series[1].data=[];
					    option.series[2].data=[];
					} 
					else if(notifyType==2){ //邮件
						option.legend.data=['邮件'];
					    option.series[0].data=[];
					    option.series[1].data =data.yMail;
					    option.series[2].data=[];
				   	}	
					else if(notifyType==1){ //电话
						option.legend.data=['电话'];
					    option.series[0].data=[];
						option.series[1].data=[];
						option.series[2].data = data.yPhone;
					}
					else{
						option.legend.data=['短信','邮件','电话'];
						option.series[0].data =data.ySMS;
						option.series[1].data =data.yMail;
						option.series[2].data = data.yPhone;
					}
					me.barChart.refash(option);
					me.enterpriseOrDriverList = data.xAxis;
				}else{
					if(notifyType==3){
						option.legend.data=['短信'];
					}else if(notifyType==2){
						option.legend.data=['邮件'];
					}else if(notifyType==1){
						option.legend.data=['电话'];
					}else{
						option.legend.data=['短信','邮件','电话'];
					}
					option.series[0].data =[];
					option.series[1].data =[];
					option.series[2].data = [];
					me.barChart.refash(option);
					//me.enterpriseOrDriverList = data.xAxis;
				}
			}
		});
	},   
	/**
     * @Description: 
     * @param vehicleId
     * @param plateNumber
     */
	queryRemindTable:function(){
		$("#resultTable").flexigrid({
			url : appCtx + "report/enterpriseRemind/loadTableData.html",
			dataType : 'json',
			colModel : remindAnalyzes.getColumn(),
			params: remindAnalyzes.getparams(),
            contentType: "application/json; charset=utf-8",
			resizable : false,
			sortname : "name",// 第一次加载数据时排序列
			sortorder : "desc",// 第一次加载数据时排序类型
			usepager : true,// 是否分页，默认为true。
			showToggleBtn : false,
			minheight: 30, //列的最小高度
			nowrap: true, //是否不换行
			showTableToggleBtn : true,// 是否显示收起/打开按钮,默认不显示。
			useRp : true,// 是否可以动态设置每页显示的结果数，默认为false。
			rp : 10,// 每页记录数，默认为10
			rpOptions: [10, 20,50, 100], // 每页结果数
			checkbox : false,// 是否要多选框,默认为false。
		//	rowId : 'vehicleId',// 多选框绑定行的id,只有checkbox : true时才有效。
			singleSelect : true,
			//widthType:"%",
			width : $(".detail-report").width(),
			height : nav.whiceH[1] -205,
            onSuccess: function (data) {
            
            },
     	});
    },
    
    getColumn : function(){
    	return [{
			display : '通知方式',
			name : 'notifyType',
			width : 120,
			sortable : true,
			align : 'left',
			paramcolnames:['notifyType'],
            handlefunction:function(notifyType){
            	if(notifyType == 1){
            		return "<span>电话</span>";
            	}else if(notifyType == 2){
            		return "<span>邮件</span>";
            	}else{
            		return "<span>短信</span>";
            	}
            }
		},{
			display : '通知对象',
			name : 'notifyObject',
			width : 120,
			sortable : true,
			align : 'left',
			paramcolnames:['notifyObject'],
            handlefunction:function(notifyObject){
            	if(notifyObject == 1){
            		return "企业";
            	}else if(notifyObject == 2){
            		return "司机";
            	}
            }
		},{
			display : '对象名称',
			name : 'name',
			width : 320,
			sortable : true,
			align : 'left'
		},{
			display : '通知内容',
			name : 'notifyContent',
			width : 400,
			sortable : true,
			align : 'left',			
		},{
			display : '通知时间',
			name : 'createTime',
			width : 260,
			sortable : true,
			align : 'left',
		}];
    },
   
    bindQuery : function(){
	    	$('#queryBtn').click(function(){
	    		//remindAnalyzes.mySetChartOption();
	    		
	    		remindAnalyzes.queryBarChartData();
	    		remindAnalyzes.tableReload();
	    	});
	    	$('#exportExcel').click(function(){
	    		remindAnalyzes.exportData();
	    	});
	    	$('#confirmExport').click(function(){
	    		remindAnalyzes.exportExcel();
	    		
	    	});
    },
    
    /**
     * 弹出导出选择页
     */
    exportData: function () {
        var options = $("#resultTable").getOptions();
        var total = options.total;
        if (total == 0) {
            global.ajaxLoading.pop("无导出数据", false, 2000);
            return;
        }
        var pages = options.pages;
        //设置默认的开始结束页
        $("#StartPageIndex").val(1);
        $("#EndPageIndex").val(pages);
        //显示导出窗口
        $("#exportModal").modal('show');
    },
    exportExcel : function (){
        	var me = this;
    		var header = JSON.stringify(me.getColumn());
    		var options = $("#resultTable").getOptions();
    		//每页大小
    		var pagesize = options.rp;
    		var headerWidth = $('.detail-report').width();
    		 var imageUrl = me.barChart.barChartDom.getDataURL({
    			type: 'png',
    		    pixelRatio: 1
    		}); 
    		//表头
    		var colModelList=new Array();
    		colModelList.push(header);
    		var colModel= $.toJSON(colModelList);
    		//图片
			var imgsrcList=new Array();
			imgsrcList.push(imageUrl);
			var imgsrc= $.toJSON(imgsrcList);
			var objId= "";
			if($('#selectDriver').is(':hidden')){
		        	objId = $("#selectEnterpriseSel").val();
		     }else{
		        	objId = $.trim($("#selectDriverSel").val());
		        }
    		var param =  {
    				startTime : $("#inputStartDate").val()+" 00:00:00",
    				endTime : $("#inputEndDate").val()+" 23:59:59",
    				notifyType : $('#notifyType').val(),
    				notifyObj:$('#remindObj').val(), //通知的对象
    		    	objId : objId,
    				colModel : colModel,
    				title : "通知分析报表",
    				imgSrc : imgsrc,
    				startPage : $("#StartPageIndex").val(),
    				endPage : $("#EndPageIndex").val(),
    				PageSize : pagesize
    		};
    		$('#exportModal').modal('hide');
    		post(appCtx+"report/enterpriseRemind/exportQueryData.html",param);
    },
	
};
$(function(){
	remindAnalyzes.init();
});


